package com.syezon.wifikey.setting;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.os.Build;

import com.blankj.utilcode.util.AppUtils;
import com.hodanet.appadvplatclient.api.HttpClient;
import com.hodanet.appadvplatclient.utils.ClientInfoUtils;
import com.syezon.wifikey.ad.AdConfig;
import com.syezon.wifikey.api.BaseHttpResult;
import com.syezon.wifikey.channel.ChannelConfig;

import org.json.JSONObject;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class FeedBackViewModel extends ViewModel {

    private static final String FEED_BACK_URL = "http://stwifi.51woshow.com/doc/feedBack.htm";

    private MutableLiveData<BaseHttpResult<Void>> feedBackResultLiveData = new MutableLiveData<>();

    public MutableLiveData<BaseHttpResult<Void>> getFeedBackResultLiveData() {
        return feedBackResultLiveData;
    }

    public void feedBack(final String qq, final String content) {
        Observable.create(new ObservableOnSubscribe<BaseHttpResult<Void>>() {
            @Override
            public void subscribe(ObservableEmitter<BaseHttpResult<Void>> e) throws Exception {
                String body = "";
                try {
                    Map<String, String> param = getDefaultParam();
                    Iterator<Map.Entry<String, String>> paramIterator = param.entrySet().iterator();
                    JSONObject paramJson = new JSONObject();
                    while (paramIterator.hasNext()) {
                        Map.Entry<String, String> next = paramIterator.next();
                        paramJson.put(next.getKey(), next.getValue());
                    }
                    paramJson.put("qq", qq);
                    paramJson.put("content", content);
                    body = paramJson.toString();
                } catch (Exception exc) {

                }
                String result = HttpClient.postResult(FEED_BACK_URL, body);
                JSONObject resultJson = new JSONObject(result);
                int rc = resultJson.optInt("rc", -1);
                int msg = resultJson.optInt("msg", -1);
                BaseHttpResult baseHttpResult = new BaseHttpResult(rc, msg, null);
                e.onNext(baseHttpResult);
            }
        })
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<BaseHttpResult<Void>>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseHttpResult<Void> voidBaseHttpResult) {
                        feedBackResultLiveData.postValue(voidBaseHttpResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        feedBackResultLiveData.postValue(null);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public static Map<String, String> getDefaultParam() {
        Map<String, String> defaultParam = new LinkedHashMap<>();
        defaultParam.put("appId", AdConfig.APP_ID + "");
        defaultParam.put("channel", ChannelConfig.getChannel());
        defaultParam.put("versionCode", AppUtils.getAppVersionCode() + "");
        defaultParam.put("versionName", AppUtils.getAppVersionName());
        defaultParam.put("imei", ClientInfoUtils.getImei());
        defaultParam.put("model", Build.MODEL);
        defaultParam.put("osVersion", Build.VERSION.SDK_INT + "");
        return defaultParam;
    }
}
